#include <windows.h>
#include "pThreads/semaphore.h"
#include <vector>
#include "CSemaphore.h"

#include "CBasicPacket.h"
#include "CChar.h"
#include "CCharSkillPacket.h"

CCharSkillPacket::CCharSkillPacket(CChar * schar){

	memset(this->data,0,0x80*2);
	this->type = 0x62;
	this->size = 0x80;

	int realcount = 0;
	schar->lockRecastAbilityList();
	for(unsigned int i = 0; i < schar->recastAbilityList.size(); i++) {
		
		int ttg = (int)(((int)schar->recastAbilityList.at(i).recast + schar->recastAbilityList.at(i).timeStamp) - time(NULL));

		if(schar->recastAbilityList.at(i).recastID != 0) {
			memcpy(this->data+4+((realcount)*4), &ttg, 3);
			memcpy(this->data+7+((realcount)*4), &schar->recastAbilityList.at(i).recastID, 1);
			realcount++;
		} else {
			
			memcpy(this->data, &ttg, 3);
		}

	}
	schar->unlockRecastAbilityList();

	unsigned short skillLevel[64];
	memset(&skillLevel, 0, sizeof(skillLevel));

	for(unsigned char i = 1; i < 48; i++) {
		if (schar->s.skillId[i]/10 >= (schar->getMaxSkill(i)) )
			skillLevel[i] = (schar->getMaxSkill(i)) + 0x8000;
		else skillLevel[i] = schar->s.skillId[i]/10;
	}

	for(unsigned char i = 48; i < 57; i++) {
		skillLevel[i] = ((schar->s.skillId[i]/10) * 0x20);
		skillLevel[i] += schar->s.rank[i];
		if( (schar->s.rank[i]+1) <= (schar->s.skillId[i]/100) )
			skillLevel[i] += 0x8000;
	}

	for(unsigned char i = 57; i < 64; i++) {
		skillLevel[i] = 0xFFFF;
	}

	memcpy(data+0x80-4, &skillLevel, sizeof(skillLevel));

}

CCharSkillPacket::~CCharSkillPacket() {

}